﻿using System.Drawing;

namespace Charting
{
    public sealed class Axis
    {
        // 所属的图表
        private Plot mChart;
        // 最小值
        private float mMinimum;
        // 最大值
        private float mMaximum;
        // 网格间隔大小
        private float mInterval;
        // 坐标轴画笔
        internal Pen AxisPen;
        // 网格画笔
        internal Pen GridPen;

        internal Axis()
        {
            mChart = null;
            mMinimum = 0;
            mMaximum = 10;
            mInterval = 1;
            AxisPen = new Pen(Color.Black);
            GridPen = new Pen(Color.Black);
        }

        internal Axis(Plot chart)
        {
            mChart = chart;
            mMinimum = 0;
            mMaximum = 10;
            mInterval = 1;
            AxisPen = new Pen(Color.Black);
            GridPen = new Pen(Color.Black);
        }

        internal void Dispose()
        {
            if (AxisPen != null)
                AxisPen.Dispose();
            if (GridPen != null)
                GridPen.Dispose();
        }

        /// <summary>
        /// 设置和获取轴的最小坐标值
        /// </summary>
        public float Minimum
        {
            set
            {
                mMinimum = value;
                ChartUpdateCoord();
                ChartUpdateSeriesPoints();
                ChartUpdateContent();
            }

            get
            {
                return mMinimum;
            }
        }

        /// <summary>
        /// 设置和获取轴的最大坐标值
        /// </summary>
        public float Maximum
        {
            set
            {
                mMaximum = value;
                ChartUpdateCoord();
                ChartUpdateSeriesPoints();
                ChartUpdateContent();
            }

            get
            {
                return mMaximum;
            }
        }

        /// <summary>
        /// 设置和获取网格间隔大小
        /// </summary>
        public float Interval
        {
            set
            {
                mInterval = value;
                ChartUpdateCoord();
                ChartUpdateContent();
            }

            get
            {
                return mInterval;
            }
        }

        /// <summary>
        /// 设置和获取网格的颜色
        /// </summary>
        public Color AxisLineColor
        {
            set
            {
                AxisPen.Color = value;
                ChartUpdateCoord();
                ChartUpdateContent();
            }

            get
            {
                return AxisPen.Color;
            }
        }

        /// <summary>
        /// 设置和获取网格线的大小
        /// </summary>
        public float AxisLineWidth
        {
            set
            {
                AxisPen.Width = value;
                ChartUpdateCoord();
                ChartUpdateContent();
            }

            get
            {
                return AxisPen.Width;
            }
        }

        /// <summary>
        /// 设置和获取网格的颜色
        /// </summary>
        public Color GridLineColor
        {
            set
            {
                GridPen.Color = value;
                ChartUpdateCoord();
                ChartUpdateContent();
            }

            get
            {
                return GridPen.Color;
            }
        }

        /// <summary>
        /// 设置和获取网格线的大小
        /// </summary>
        public float GridLineWidth
        {
            set
            {
                GridPen.Width = value;
                ChartUpdateCoord();
                ChartUpdateContent();
            }

            get
            {
                return GridPen.Width;
            }
        }

        private void ChartUpdateCoord()
        {
            if (mChart == null)
                return;
            mChart.UpdateCoord();
        }

        private void ChartUpdateSeriesPoints()
        {
            if (mChart == null)
                return;
            mChart.UpdateSeriesPoints();
        }

        private void ChartUpdateContent()
        {
            if (mChart == null)
                return;
            mChart.UpdateContent();
        }
    }
}
